package server.web.api.webuser;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import server.Main;
import server.web.WebUser;

public class SendMsg extends HttpServlet {
    private static final long serialVersionUID = -7288159219847209829L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");

        HttpSession se = req.getSession(false);
        if (se != null && se.getAttribute("user") != null) {
            String[] msg = req.getParameterValues("msg");
            if (msg != null && msg.length == 1) {
                WebUser user = (WebUser) se.getAttribute("user");
                String msg0 = URLDecoder.decode(msg[0], "utf-8");
                Main.chatServer.msgReceived(msg0, user);
                resp.getWriter().print("true");
            } else {
                resp.getWriter().print("false");
            }
        } else {
            resp.getWriter().print("false");
        }
    }
}